package com.wskh.eduOrder.service.impl;

import com.wskh.eduOrder.client.EduClient;
import com.wskh.eduOrder.client.UcenterClient;
import com.wskh.eduOrder.entity.CourseWebVo;
import com.wskh.eduOrder.entity.TOrder;
import com.wskh.eduOrder.entity.UcenterMember;
import com.wskh.eduOrder.mapper.TOrderMapper;
import com.wskh.eduOrder.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wskh.eduOrder.util.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wskh
 * @since 2021-10-22
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    @Autowired
    EduClient eduClient;
    @Autowired
    UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String memberIdByJwtToken) {

        UcenterMember member = ucenterClient.getInfoUc(memberIdByJwtToken);

        CourseWebVo courseInfoDto = eduClient.getCourseInfoDto(courseId);

        TOrder tOrder = new TOrder();

        //创建订单
        TOrder order = new TOrder();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfoDto.getTitle());
        order.setCourseCover(courseInfoDto.getCover());
        order.setTeacherName("test");
        order.setTotalFee(courseInfoDto.getPrice());
        order.setMemberId(memberIdByJwtToken);
        order.setMobile(member.getMobile());
        order.setNickname(member.getNickname());
        order.setStatus(0);
        // 默认为已支付状态 方便测试
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
